<?php use_helper('Object', 'Validation', 'ObjectAdmin', 'I18N', 'Date') ?>
<title><?= $sf_response->getTitle() ?></title>
<?php use_stylesheet('/sf/sf_admin/css/main') ?>

<div id="sf_admin_container">

<h1>Resolve <?php echo isset($mr) && $mr? $mr->getMrno() : ''; echo $module=='MR'? ':'.$mr->getProject():''; ?></h1>

<div id="sf_admin_content">

<?php echo form_tag('po/resolve', array(
  'id'        => 'sf_admin_edit_form',
  'name'      => 'sf_admin_edit_form',
  'multipart' => true,
)) ?>

<input type="hidden" name="mr_id" id="mr_id" value="<?php echo $mr->getId()?>" />
<input type="hidden" name="mod" id="mod" value="<?php echo $module?>" />

<div id ="list_component">
          <table id ='ListOfComponents' "cellspacing="0" class="sf_admin_list">
            <thead>
            <tr>
                  <th id="sf_admin_list_th_productcode" width="15%">Product Code
                          </th>
                  <th id="sf_admin_list_th_description" width="30%">Description
                          </th>
                  <th id="sf_admin_list_th_description" ><? echo $mr->getMrno()?>
                          </th>
                          <?
                          $c = new Criteria();
                       $c->add(POPeer::MRNO, $mr->getReviseId());
                       if ($module =='MR')
                           $c->add(POPeer::COMPCATEGORY, CommonValues::$gCompCategory['key']['Main Component']);
                       elseif ($module == 'MRSUB')
                           $c->add(POPeer::COMPCATEGORY, CommonValues::$gCompCategory['key']['Sub Component']);
                       else $c->add(POPeer::COMPCATEGORY, CommonValues::$gCompCategory['key']['Raw Material']);
                       
                       $c->addOr(POPeer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
                       $c->addOr(POPeer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Initial']);
                       $c->addOr(POPeer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Waiting for approval']);
                       $polist = POPeer::doSelect($c);
                       if ($polist && count($polist)){
                           foreach ($polist as $po){
                               echo "<th id='sf_admin_list_th_description' >".$po->getPono()."</th>";
                           }
                       }
                       ?>
            </tr>
            </thead>


            <tr class="sf_admin_row_0">
                  <td id="sf_admin_list_th_productcode" width="15%">
                          </td>
                  <td id="sf_admin_list_th_description" width="30%">
                          </td>
                  <td id="sf_admin_list_th_description" >
                          </td>
                          <?

                       if ($polist && count($polist)){
                           foreach ($polist as $po){
                               echo "<td id='sf_admin_list_th_description' >".CommonValues::$gMRApprovalStatus['value'][$po->getApprovalstatus()]."</td>";
                           }
                       }
                       ?>
            </tr>
            <tr class="sf_admin_row_0">
                  <td id="sf_admin_list_th_productcode" width="15%">
                          </td>
                  <td id="sf_admin_list_th_description" width="30%">
                          </td>
                  <td id="sf_admin_list_th_description" >
                          </td>
                          <?
                          
                       if ($polist && count($polist)){
                           foreach ($polist as $po){
                               echo "<td id='sf_admin_list_th_description' >
                                   <select name='action".$po->getId()."' id='action".$po->getId()."'>
                                    <option value='0'>Keep</option>
                                    <option value='1'>Revise</option>
                                    <option value='2'>Delete</option>
                                    </select>"."</td>";
                           }
                       }
                       ?>
            </tr>

            <?
            if ($module =='MR')
                $rvMR = MRPeer::retrieveByPK($mr->getReviseId());
            elseif ($module=='MRSUB')
                $rvMR = MR2Peer::retrieveByPK($mr->getReviseId());
            else $rvMR = MR3Peer::retrieveByPK($mr->getReviseId());

            if (!$rvMR)
            {
                echo "ERROR: khong co revise mr";
                exit();
            }
            $c = new Criteria();
            $c->add(ComponentsPeer::COMPONENTGROUP_ID, $rvMR->getComponentgroupId());
            $c->addOr(ComponentsPeer::COMPONENTGROUP_ID, $mr->getComponentgroupId());
            $compslist = ComponentsPeer::doSelect($c);

            $dem =0;

            if ($compslist && count($compslist)){                
                foreach ($compslist as $comps){
                    $ListComp[$comps->getComponentId()] = 1;
                }
            }
            if (isset($ListComp) && $ListComp && count($ListComp)){
                
                foreach ($ListComp as $compId=>$val){
                    $comp = ComponentPeer::retrieveByPK($compId);
                    $dem++;
            ?>

                 <tr class="sf_admin_row_<?echo $dem%2?>">
                     <input type="hidden" name="rowIds[]" id ="rowIds"  class = "ExistComponents" value="<? echo $compId; ?>"/>
                      <td id="sf_admin_list_th_productcode" width="15%"><? echo $comp->getProductcode()?>
                              </td>
                      <td id="sf_admin_list_th_description" width="30%"><? echo $comp->getDescription()?>
                              </td>
                      <td id="sf_admin_list_th_mr" >
                          <?
                            $c = new Criteria();
                            $c->add(ComponentsPeer::COMPONENTGROUP_ID, $mr->getComponentgroupId());
                            $c->add(ComponentsPeer::COMPONENT_ID, $compId);
                            $MRcompsx = ComponentsPeer::doSelectOne($c);
                            if ($MRcompsx)
                                echo $MRcompsx->getQty1();
                            else echo 0;
                          ?>
                              </td>
                          <?
                    if ($polist && count($polist)){
                       foreach ($polist as $po){
                           $c = new Criteria();
                           $c->add(ComponentsPeer::COMPONENTGROUP_ID, $po->getComponentgroupId());
                           $c->add(ComponentsPeer::COMPONENT_ID, $compId);

                           $POcomps = ComponentsPeer::doSelectOne($c);
                           ?>
                           <td id="sf_admin_list_th_po" >
                               <input type="text" name="comps<?echo $po->getId()?>[]" id ="comps<?
                                  echo $po->getId()?>" size="5" value ="<?

                                   if ($POcomps){
                                       echo $POcomps->getQty1();
                                   }
                                ?>">
                           </td>
                           <?
                       }
                    }
                    ?>

                 </tr>

                    <?
                }
            }
           ?>
          </table>
</div>

<ul class="sf_admin_actions">
    <li><?php echo submit_tag('Resolve', array (
  'name' => 'save',
  'class' => 'sf_admin_action_save',
    'confirm' => __('Are you sure you want to resolve?'),
)) ?></li>
</ul>
</form>
</div>
</div>